/*
 PureMVC AS3 Demo - Flex Application Skeleton 
 Copyright (c) 2007 Daniele Ugoletti <daniele.ugoletti@puremvc.org>
 Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package com.larson.creator.view
{
	import com.larson.creator.*;
	import com.larson.creator.model.*;
	import com.larson.creator.view.components.*;
	
	import flash.events.Event;
	
	import mx.collections.*;
	import mx.events.MenuEvent;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.Mediator;
    
    /**
     * A Mediator for interacting with the top level Application.
     * 
     * <P>
     * In addition to the ordinary responsibilities of a mediator
     * the MXML application (in this case) built all the view components
     * and so has a direct reference to them internally. Therefore
     * we create and register the mediators for each view component
     * with an associated mediator here.</P>
     * 
     * <P>
     * Then, ongoing responsibilities are: 
     * <UL>
     * <LI>listening for events from the viewComponent (the application)</LI>
     * <LI>sending notifications on behalf of or as a result of these 
     * events or other notifications.</LI>
     * <LI>direct manipulating of the viewComponent by method invocation
     * or property setting</LI>
     * </UL>
     */
    public class ApplicationMediator extends Mediator implements IMediator
    {
        // Cannonical name of the Mediator
        public static const NAME:String = "ApplicationMediator";
        
		// available values for the main viewstack
		// defined as contants to help uncover errors at compile time instead of run time
		public static const DATA_SCREEN : Number 		=	0;
		public static const BUILD_SCREEN : Number 		=	1;
		public static const PROFILE_SCREEN : Number 	=	2;
		public static const COURSE_SCREEN : Number 		=	3;
		//public static const BUILD_SCREEN : Number 		=	4;
        
       /**
         * Constructor. 
         * 
         * <P>
         * On <code>applicationComplete</code> in the <code>Application</code>,
         * the <code>ApplicationFacade</code> is initialized and the 
         * <code>ApplicationMediator</code> is created and registered.</P>
         * 
         * <P>
         * The <code>ApplicationMediator</code> constructor also registers the 
         * Mediators for the view components created by the application.</P>
         * 
         * @param object the viewComponent (the ApplicationSkeleton instance in this case)
         */
        public function ApplicationMediator( viewComponent:CourseCreator ) 
        {
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            super( NAME, viewComponent );

            // Create and register Mediators
            // components that were instantiated by the mxml application 
            //facade.registerMediator( new controlScreenMediator ( app.controls ) );
			//facade.registerMediator( new mapMediator( app.mapScreen ) );
			facade.registerMediator( new profileMediator( app.profileScreen ) );
			facade.registerMediator( new crsMediator( app.crsScreen ) );
			facade.registerMediator( new dataMediator( app.dataScreen ) );
			facade.registerMediator( new buildMediator( app.buildScreen ) );
			
			app.addEventListener(CourseCreator.MAIN_APP_READY, this.initMenu);

		}

        /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
            return [
						ApplicationFacade.VIEW_MAP_SCREEN,
						ApplicationFacade.VIEW_PROFILE_SCREEN,
						ApplicationFacade.VIEW_COURSE_SCREEN,
						ApplicationFacade.VIEW_DATA_SCREEN,
						ApplicationFacade.VIEW_BUILD_SCREEN,
						ApplicationFacade.LOAD_FILE_COMPLETE,
						ApplicationFacade.DATA_READY			
					];
        }

        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
/*				case ApplicationFacade.VIEW_MAP_SCREEN:
					app.tabNav.selectedIndex = MAP_SCREEN;
					break;
*/
				case ApplicationFacade.VIEW_PROFILE_SCREEN:
					app.tabNav.selectedIndex = PROFILE_SCREEN;
					break;
					
				case ApplicationFacade.VIEW_COURSE_SCREEN:
					app.tabNav.selectedIndex = COURSE_SCREEN;
					break;
					
				case ApplicationFacade.VIEW_DATA_SCREEN:
					app.tabNav.selectedIndex = DATA_SCREEN;
					break;
					
				case ApplicationFacade.VIEW_BUILD_SCREEN:
					app.tabNav.selectedIndex = BUILD_SCREEN;
					break;
				case ApplicationFacade.LOAD_FILE_COMPLETE:
					// Get the file load proxy, and the profile controls proxy
					var fileLoad:FileLoadProxy = ApplicationFacade.getFileLoadProxy();
					var profileControls:ProfileControlsProxy = ApplicationFacade.getProfileControlsProxy();
					fileLoad.createRawData( fileLoad.trackData, profileControls.profileControls );
					break;
				case ApplicationFacade.DATA_READY:
					// Get the file load proxy, and the profile controls proxy
					var loader:FileLoadProxy = ApplicationFacade.getFileLoadProxy();
					var controls:ProfileControlsProxy = ApplicationFacade.getProfileControlsProxy();
					var converter:FileConvertProxy = ApplicationFacade.getFileConvertProxy();
					converter.processData( loader.trackData, loader.lineData, controls.profileControls );
					break;            
			}
        }

        /**
         * Cast the viewComponent to its actual type.
         * 
         * <P>
         * This is a useful idiom for mediators. The
         * PureMVC Mediator class defines a viewComponent
         * property of type Object. </P>
         * 
         * <P>
         * Here, we cast the generic viewComponent to 
         * its actual type in a protected mode. This 
         * retains encapsulation, while allowing the instance
         * (and subclassed instance) access to a 
         * strongly typed reference with a meaningful
         * name.</P>
         * 
         * @return app the viewComponent cast to AppSkeleton
         */
        protected function get app():CourseCreator
		{
            return viewComponent as CourseCreator
        }
        
        // Event handler to initialize the MenuBar control.
        private function initMenu(event:Event):void {
        	app.mainMenu.addEventListener(MenuEvent.ITEM_CLICK, this.menuClick);
        }
        
		private function menuClick(event:MenuEvent):void {
			var action:String = event.item.@label;
            switch ( action ) 
			{
				case CourseCreator.VIEW_MAP:
					app.tabNav.selectedIndex = BUILD_SCREEN;
					break;

				case CourseCreator.VIEW_PROFILE:
					app.tabNav.selectedIndex = PROFILE_SCREEN;
					break;
					
				case CourseCreator.VIEW_RAW_DATA:
					app.tabNav.selectedIndex = DATA_SCREEN;
					break;
					
				case CourseCreator.OPEN_FILE:
					this.sendNotification( ApplicationFacade.COMMAND_FILE_LOAD);
					break;
					
				case CourseCreator.NEW_FILE:
					app.tabNav.selectedIndex = BUILD_SCREEN;
					break;					
            }
			
			
		}
        
        
    }
}